package info.cs52.fes.dao;

import java.util.List;

import info.cs52.fes.Application;
import info.cs52.fes.data.Recipe;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

public class RecipeDAO {

	public static Recipe findByName(String name) {
		EntityManager em = Application.getEntityManager();
		Query query = em.createQuery("select d from Recipe d where name=:name");
		query.setParameter("name", name);
		List recipes = query.getResultList();
		if (recipes.size() == 1) {
			return (Recipe) recipes.get(0);
		}
		return null;
	}

	public static Recipe find(String id) {
		EntityManager em = Application.getEntityManager();
		return em.find(Recipe.class, id);
	}

	public static void persist(Recipe dish) {
		EntityManager em = Application.getEntityManager();
		em.persist(dish);
	}

	public static void delete(String id) {
		EntityManager em = Application.getEntityManager();
		em.remove(em.getReference(Recipe.class, id));
	}

	public static List<Recipe> list() {
		EntityManager em = Application.getEntityManager();
		Query query = em.createQuery("from Recipe");
		return query.getResultList();
	}

}
